#include <windows.h>
#include <windowsx.h>
#include <gdiplus.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

using namespace Gdiplus;

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
					 LPSTR lpszCmdParam,int nCmdShow)
{
	HWND hWnd;
	MSG Message;
	WNDCLASS WndClass;
	LPSTR lpszClass = "im2";

	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	WndClass.cbClsExtra=0;
	WndClass.cbWndExtra=0;
	WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
	WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = (WNDPROC)WndProc;
	WndClass.lpszClassName = lpszClass;
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&WndClass);

	hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,(HMENU)NULL,hInstance,
		NULL);
	ShowWindow(hWnd,nCmdShow);

	while(GetMessage(&Message,0,0,0)) {
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}

	GdiplusShutdown(gdiplusToken);
	return static_cast<int>(Message.wParam);
}

int cx, cy;

void OnSize(HWND hwnd, UINT state, int cx, int cy);
void OnPaint(HWND hwnd);
void OnDestroy(HWND hwnd);

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		HANDLE_MSG(hwnd, WM_SIZE, OnSize);
		HANDLE_MSG(hwnd, WM_PAINT, OnPaint);
		HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy);
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

void OnSize(HWND hwnd, UINT state, int cx, int cy)
{
	::cx = cx;
	::cy = cy;
}

void OnPaint(HWND hwnd)
{
	PAINTSTRUCT ps;
	HDC hdc;

	hdc = BeginPaint(hwnd, &ps);

	{
		RECT rcTemp;
		GetClientRect(hwnd, &rcTemp);

		Rect rc;
		rc.X = 0;
		rc.Y = 0;
		rc.Width = rcTemp.right;
		rc.Height = rcTemp.bottom;

		SolidBrush brRed(Color(127, 255, 0, 0));

		Graphics graphics(hdc);
		graphics.FillRectangle(&brRed, rc);
		EndPaint(hwnd, &ps);
	}
}

void OnDestroy(HWND hwnd)
{
	PostQuitMessage(0);
}